﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.job
{
    public partial class job_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString();//操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.jobs().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                GetCategoryID();
                GetQuanziID();
                //ChkAdminLevel("sys_site_manage", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定类别=================================
        private void GetCategoryID()
        {
            BLL.job_category bll = new BLL.job_category();
            DataTable dt = bll.GetList(0, "","sort_id asc, id desc").Tables[0];

            this.ddlCategoryId.Items.Clear();
            this.ddlCategoryId.Items.Add(new ListItem("请选择类别...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();
                this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定圈子=================================
        private void GetQuanziID()
        {
            BLL.quanzi bll = new BLL.quanzi();
            DataTable dt = bll.GetList(0, "", "sort_id asc, id desc").Tables[0];

            this.ddlQuanzi.Items.Clear();
            this.ddlQuanzi.Items.Add(new ListItem("请选择所属圈子...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();
                this.ddlQuanzi.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.jobs bll = new BLL.jobs();
            Model.jobs model = bll.GetModel(_id);

            rblStatus.SelectedValue = model.status.ToString();
            rblTypeID.SelectedValue = model.type_id.ToString();
            txtTitle.Text = model.title;
            ddlCategoryId.SelectedValue = model.category_id.ToString();
            ddlQuanzi.SelectedValue = model.quanzi_id.ToString();
            txtContent.Text = model.content;
            txtLianxiren.Text = model.lianxiren;
            txtMobile.Text = model.mobile;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.jobs model = new Model.jobs();
            BLL.jobs bll = new BLL.jobs();

            int quanzi_id= Utils.StrToInt(ddlQuanzi.SelectedValue, 0);
            int province_id = 0;
            int city_id = 0;
            int district_id = 0;
            if (quanzi_id > 0)
            {
                Model.quanzi qzModel = new BLL.quanzi().GetModel(quanzi_id);
                province_id = qzModel.province_id;
                city_id = qzModel.city_id;
                district_id = qzModel.district_id;
            }
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);
            model.type_id = Utils.StrToInt(rblTypeID.SelectedValue, 1);
            model.title = txtTitle.Text.Trim();
            model.category_id = Utils.StrToInt(ddlCategoryId.SelectedValue, 0);
            model.quanzi_id = quanzi_id;
            model.province_id = province_id;
            model.city_id = city_id;
            model.district_id = district_id;
            model.user_id = 0;
            model.company_id = 0;
            model.content = txtContent.Text;
            model.lianxiren = txtLianxiren.Text.Trim();
            model.mobile = txtMobile.Text.Trim();
            model.add_time = DateTime.Now;

            if (bll.Add(model) > 0)
            {
                return true;
            }

            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.jobs bll = new BLL.jobs();
            Model.jobs model = bll.GetModel(_id);

            int quanzi_id = Utils.StrToInt(ddlQuanzi.SelectedValue, 0);
            int province_id = 0;
            int city_id = 0;
            int district_id = 0;
            if (quanzi_id > 0)
            {
                Model.quanzi qzModel = new BLL.quanzi().GetModel(quanzi_id);
                province_id = qzModel.province_id;
                city_id = qzModel.city_id;
                district_id = qzModel.district_id;
            }
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);
            model.type_id = Utils.StrToInt(rblTypeID.SelectedValue, 1);
            model.title = txtTitle.Text.Trim();
            model.category_id = Utils.StrToInt(ddlCategoryId.SelectedValue, 0);
            model.quanzi_id = quanzi_id;
            model.province_id = province_id;
            model.city_id = city_id;
            model.district_id = district_id;
            model.user_id = 0;
            model.company_id = 0;
            model.content = txtContent.Text;
            model.lianxiren = txtLianxiren.Text.Trim();
            model.mobile = txtMobile.Text.Trim();

            if (bll.Update(model))
            {
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                //ChkAdminLevel("sys_site_manage", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改信息成功！", "job_list.aspx", "parent.loadMenuTree");
            }
            else //添加
            {
                //ChkAdminLevel("sys_site_manage", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加信息成功！", "job_list.aspx", "parent.loadMenuTree");
            }
        }
    }
}